﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using LH.Model.ST;
using XXX.Bo;
using System.ServiceModel;
using ServiceReference1;
using XXX.Common;
using System.Xml.Linq;
using LH.Model.PU;
using LH.Model.APAR;

namespace U8COWeb
{
    /// <summary>
    /// 付款申请单单据接口
    /// </summary>
    public static class AP_APPLAYVOUCHDALCO
    {


        #region 新增付款申请单 
        /// <summary>
        /// 新增付款申请单,返回ok或者error
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="err">错误信息</param>
        /// <param name="id">新增单据的id</param>
        /// <param name="ccode">新增单据的code</param>
        /// <returns></returns>
        public static bool Add(AP_ApplyVouch entity, string xmlconfig, out string err, out string id, out string ccode)
        {
            try
            {
                string xdomhead = XmlOperation.GetCOXmlHead<AP_ApplyVouch.Head>(entity.head);
                string xdombody = XmlOperation.GetCOXmlBody<AP_ApplyVouch.Body>(entity.bodies);

                var binding = new BasicHttpBinding();
                binding.MaxReceivedMessageSize = 2147483647;
                // 根据 WebService 的 URL
                var endpoint = new EndpointAddress(CanShu.coUrl);
                // 创建调用接口的工厂，注意这里泛型只能传入接口
                var factory = new ChannelFactory<WebService1Soap>(binding, endpoint);
                // 从工厂获取具体的调用实例
                var callClient = factory.CreateChannel();

                var result = callClient.execAsync("AP", "付款申请", "add", "", xdomhead, xdombody, "", xmlconfig, "config").Result;

                StringBuilder sb = new StringBuilder();
                sb.AppendLine("cSubID=" + "AP");
                sb.AppendLine("cVouchType=" + "付款申请");
                sb.AppendLine("cOptType=" + "add");
                sb.AppendLine("cVouchID=" + "");
                sb.AppendLine("domHead=" + xdomhead);
                sb.AppendLine("domBody=" + xdombody);
                sb.AppendLine("sErrorMsg=" + "");
                sb.AppendLine("domSub=" + xmlconfig);
                sb.AppendLine("other=" + "config");
                sb.AppendLine("result=" + result.ToString());
                ExLog.WriteTxt("CO", sb.ToString());
                /*var xmlResult = XDocument.Parse(result); //实例化XmlDocument,怎么用这个,网上去查查*/
                return XmlOperation.GetCoResult("新增付款申请单", "1", out err, out id, out ccode, result);
            }
            catch (Exception ex)
            {
                id = "";
                ccode = "";
                err = ex.Message;
                return false;
            }
        }

        public static bool AddVerify(AP_ApplyVouch entity, string xmlconfig, out string err, out string id, out string ccode)
        {
            try
            {
                string xdomhead = XmlOperation.GetCOXmlHead<AP_ApplyVouch.Head>(entity.head);
                string xdombody = XmlOperation.GetCOXmlBody<AP_ApplyVouch.Body>(entity.bodies);

                var binding = new BasicHttpBinding();
                binding.MaxReceivedMessageSize = 2147483647;
                // 根据 WebService 的 URL
                var endpoint = new EndpointAddress(CanShu.coUrl);
                // 创建调用接口的工厂，注意这里泛型只能传入接口
                var factory = new ChannelFactory<WebService1Soap>(binding, endpoint);
                // 从工厂获取具体的调用实例
                var callClient = factory.CreateChannel();

                //付款申请单不可以加DP
                var result = callClient.execAsync("AP", "付款申请", "add,verify", "", xdomhead, xdombody, "", xmlconfig, "config").Result;

                StringBuilder sb = new StringBuilder();
                sb.AppendLine("cSubID=" + "AP");
                sb.AppendLine("cVouchType=" + "付款申请");
                sb.AppendLine("cOptType=" + "add");
                sb.AppendLine("cVouchID=" + "");
                sb.AppendLine("domHead=" + xdomhead);
                sb.AppendLine("domBody=" + xdombody);
                sb.AppendLine("sErrorMsg=" + "");
                sb.AppendLine("domSub=" + xmlconfig);
                sb.AppendLine("other=" + "config");
                sb.AppendLine("result=" + result.ToString());
                ExLog.WriteTxt("CO", sb.ToString());
                /*var xmlResult = XDocument.Parse(result); //实例化XmlDocument,怎么用这个,网上去查查*/
                return XmlOperation.GetCoResult("新增付款申请单", "1", out err, out id, out ccode, result);
            }
            catch (Exception ex)
            {
                id = "";
                ccode = "";
                err = ex.Message;
                return false;
            }
        }

       
        #endregion

        #region 审核/弃审/删除付款申请单
        /// <summary>
        /// 审核/弃审/删除付款申请单
        /// </summary>
        /// <param name="ID">出库单ID</param>
        /// <param name="u8login">登录对象</param>
        /// <param name="conn">数据库连接对象</param>
        /// <param name="errMsg">错误信息</param>
        /// <param name="coperation">操作：delete，verify，unverify</param>
        /// <returns></returns>
        public static bool Operate(String ID, string xmlconfig, string coperation, out string err)
        {
            try
            {

                var binding = new BasicHttpBinding();
                binding.MaxReceivedMessageSize = 2147483647;
                // 根据 WebService 的 URL
                var endpoint = new EndpointAddress(CanShu.coUrl);
                // 创建调用接口的工厂，注意这里泛型只能传入接口
                var factory = new ChannelFactory<WebService1Soap>(binding, endpoint);
                // 从工厂获取具体的调用实例
                var callClient = factory.CreateChannel();
                // 调用具体的方法，这个地方就看你都有啥方法啦，自行替换
                //var request = new SendFormatSMSRequest(sendFormatSMSRequestBody);

                var result = callClient.execAsync("AP", "付款申请", coperation, ID.ToString(), "", "", "", xmlconfig, "config").Result;

                var xmlResult = XDocument.Parse(result); //实例化XmlDocument,怎么用这个,网上去查查
                var node = xmlResult.Descendants("return");
                var resultcode = node.FirstOrDefault().Attribute("bRet").Value;
                var resultmes = node.FirstOrDefault().Attribute("sError").Value;
                if (resultcode == "False")
                {

                    err = resultmes;
                    return true;
                }
                else
                {

                    err = "";
                    return true;
                }
            }
            catch (Exception ex)
            {

                err = ex.Message;
                return false;
            }
        }

         

        #endregion




    }
}
